
//
//  GNRSelectionCardView.m
//  GNRPickCard_Example
//
//  Created by LvYuan on 2017/11/1.
//  Copyright © 2017年 ly918@qq.com. All rights reserved.
//

#import "GNRSelectionCardView.h"

@interface GNRSelectionCardView()<UITableViewDelegate,UITableViewDataSource>
{
    CGFloat headerViewHeight;
    CGFloat footerViewHeight;
}
@property (nonatomic, strong)NSString *registerCellID;
@property (nonatomic, strong)NSString *registerCellNibName;
@end

@implementation GNRSelectionCardView

- (void)reloadData{
    [self.tableView reloadData];
}

- (void)configTableView{
    
    self.tableView.rowHeight = [_config_UI respondsToSelector:@selector(cellHeight)]?_config_UI.cellHeight:0;
    self.headerView = [self.config_UI respondsToSelector:@selector(headerView)]?self.config_UI.headerView:nil;
    self.footerView = [self.config_UI respondsToSelector:@selector(footerView)]?self.config_UI.footerView:nil;
    
    BOOL hideCellSeparatorLine = NO;
    if ([self.config_UI respondsToSelector:@selector(hideCellSeparatorLine)]) {
        hideCellSeparatorLine = self.config_UI.hideCellSeparatorLine;
    }
    self.tableView.separatorStyle = hideCellSeparatorLine?UITableViewCellSeparatorStyleNone:UITableViewCellSeparatorStyleSingleLine;
    
    headerViewHeight = [self.config_UI respondsToSelector:@selector(headerHeight)]?self.config_UI.headerHeight:CGFLOAT_MIN;
    footerViewHeight = [self.config_UI respondsToSelector:@selector(footerHeight)]?self.config_UI.footerHeight:CGFLOAT_MIN;
    
    if ([_config_UI respondsToSelector:@selector(cellId)]) {
        _registerCellID = _config_UI.cellId;
    }
    if ([_config_UI respondsToSelector:@selector(cellNibName)]) {
        _registerCellNibName = _config_UI.cellNibName;
    }
    if (_registerCellNibName.length && _registerCellID.length) {
        [self.tableView registerNib:[UINib nibWithNibName:_registerCellNibName bundle:nil] forCellReuseIdentifier:_registerCellID];
    }
    NSAssert(_registerCellNibName.length, @"未配置相应的CellNibName！");
}

- (UIView *)footerView{
    if (_footerView) {
        UITapGestureRecognizer *tapPress = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(footerViewPressed:)];
        [_footerView addGestureRecognizer:tapPress];
    }
    return _footerView;
}

- (UIView *)headerView{
    if (_headerView) {
        UITapGestureRecognizer *tapPress = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerViewPressed:)];
        [_headerView addGestureRecognizer:tapPress];
    }
    return _headerView;
}

- (void)setConfig_UI:(id<GNRSelectionCardConfigProtocol>)config_UI{
    _config_UI = config_UI;
    NSAssert(config_UI, @"未配置GNRSelectionCardConfigProtocol！");
    [self configTableView];
    [self reloadData];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
}

- (void)setItems:(NSMutableArray<id<GNRSelectionItemProtocol>> *)items{
    _items = items;
    [self reloadData];
}

- (id<GNRSelectionItemProtocol>)itemWithIndexPath:(NSIndexPath *)indexPath{
    id<GNRSelectionItemProtocol> item =nil;
    if (self.items.count ) {
        item = self.items[indexPath.row%self.items.count];
    }
    return item;
}

//MARK: - TableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.items.count?1:0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID = _registerCellID;
    if (cellID.length) {
        GNRBaseSelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        id<GNRSelectionItemProtocol>item = [self itemWithIndexPath:indexPath];
        cell.selectionItem = item;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    NSAssert(cellID.length, @"未配置相应的CellID！");
    return [UITableViewCell new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    id<GNRSelectionItemProtocol>item = [self itemWithIndexPath:indexPath];
    [self selectedItem:item];
    if (_selectBlock) {
        _selectBlock(item);
    }
}

- (void)selectedItem:(id<GNRSelectionItemProtocol>)item{
    [self.items enumerateObjectsUsingBlock:^(id<GNRSelectionItemProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
    }];
    item.selected = YES;
    [self reloadData];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return self.footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return footerViewHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return headerViewHeight;
}

- (void)footerViewPressed:(id)sender{
    if (_selectFooterBlock) {
        _selectFooterBlock(self.footerView);
    }
}

- (void)headerViewPressed:(id)sender{
    if (_selectHeaderBlock) {
        _selectHeaderBlock(self.headerView);
    }
}

@end
